package mycoffee.order.xmlhandler;

import java.util.ArrayList;

import mycoffee.order.model.Category;
import mycoffee.order.model.Food;
import mycoffee.order.model.SubCategory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.text.TextUtils;

public class FoodsHandler extends DefaultHandler {
	/** list of categories. */
	private ArrayList<Category> mCategories;
	private Category mCurrentCategory = null;
	private SubCategory mCurrentSubCategory = null;

	public FoodsHandler() {
		mCategories = new ArrayList<Category>();
	}

	public ArrayList<Category> getResult() {
		return mCategories;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		try {
			if (TextUtils.equals(localName, XmlConstants.CATEGORY)) {
				mCurrentCategory = new Category();
				mCategories.add(mCurrentCategory);
				mCurrentCategory.setId(Integer.parseInt(attributes
						.getValue(XmlConstants.ID)));
				mCurrentCategory
						.setName(attributes.getValue(XmlConstants.NAME));
				mCurrentCategory.setName_vi(attributes
						.getValue(XmlConstants.NAME_VN));
				mCurrentCategory.setName_en(attributes
						.getValue(XmlConstants.NAME_EN));
				mCurrentCategory.setName_de(attributes
						.getValue(XmlConstants.NAME_DE));
			} else if (TextUtils.equals(localName, XmlConstants.SUBCATEGORY)) {
				if (mCurrentCategory == null) {
					return;
				}
				mCurrentSubCategory = new SubCategory();
				// get current list subcategories.
				ArrayList<SubCategory> subCategories = mCurrentCategory
						.getSubCategories();
				if (subCategories != null) {
					subCategories.add(mCurrentSubCategory);
					// add information.
					mCurrentSubCategory.setId(Integer.parseInt(attributes
							.getValue(XmlConstants.ID)));
					mCurrentSubCategory.setName(attributes
							.getValue(XmlConstants.NAME));
					mCurrentSubCategory.setName_vi(attributes
							.getValue(XmlConstants.NAME_VN));
					mCurrentSubCategory.setName_en(attributes
							.getValue(XmlConstants.NAME_EN));
					mCurrentSubCategory.setName_de(attributes
							.getValue(XmlConstants.NAME_DE));
					mCurrentSubCategory.setCategory_id(mCurrentCategory.getId());
				}

			} else if (TextUtils.equals(localName, XmlConstants.FOOD)) {
				if (mCurrentSubCategory == null) {
					return;
				}
				// create new food.
				Food food = new Food();
				ArrayList<Food> foods = mCurrentSubCategory.getFoods();
				if (foods != null) {
					foods.add(food);
					// add info.
					food.setId(Integer.parseInt(attributes
							.getValue(XmlConstants.ID)));
					food.setName(attributes.getValue(XmlConstants.NAME));
					food.setName_vi(attributes.getValue(XmlConstants.NAME_VN));
					food.setName_en(attributes.getValue(XmlConstants.NAME_EN));
					food.setName_de(attributes.getValue(XmlConstants.NAME_DE));
					food.setImage(attributes.getValue(XmlConstants.IMAGE));
					food.setPrice(Double.parseDouble(attributes
							.getValue(XmlConstants.PRICE)));
					food.setSubcategoryID(mCurrentSubCategory.getId());
					//food.setCategoryID(mCurrentCategory.getId());
				}

			}
		} catch (Exception e) {
		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
	}
}
